/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 014.c
 * buy a house
 ***********************************************************************************************/

#include <stdio.h>

/*
 * I split this max question into several single questions. that is to say, this program only
 * count 1 condition once, lump-sum, 5-year installment, 10-year or 20-year.
 * then it becomes a function called countEarn, which can be called circularly, to deal with
 * an array of conditions.
 * in addition, it startles me bcz the rate is too high to get an unbelievable repay when
 * save it into bank, oh, in those days. 
 * there is still a problem if rates are different between periods. for example, we can leave
 * 5000RMB for 1 year deposit and first 5 years' installment, and then save 14000RMB for 5
 * years deposit. that is obviously better then save all 19000 for 1 year deposit in five years.
 * oh, that's too complicated, so I don't include different rate of money saving.
 */

double conditions[4][4] = {
	20000, 1, 0.2, 0.0063,
	20000, 5, 0.15, 0.0063,
	20000, 10, 0.1, 0.0063,
	20000, 20, 0, 0.0063};

int countEarn(double total, double y, double discount, double rate)
{
	int year = (int)y;
	printf("Total = %f, Year = %d, Discount = %f, Rate = %f\n", total, year, discount, rate);
	double temp_total = 0.0, earn = 0.0;

	int i = 0;
	temp_total = total - (total/year)*(1-discount);
	for (i = 1; i < year; i++)
	{
                printf("--%f is saved into bank, ", temp_total);
		earn = temp_total * rate * 12;
		temp_total = temp_total + earn - (total/year)*(1-discount);
		printf("earn %f in this year and %f left after installment\n", earn, temp_total);
	}
	printf("%f will be saved\n", temp_total);
	return(0);
}

int main()
{
	int i = 0;
	for (i = 0; i < 4; i++)
		countEarn(conditions[i][0], conditions[i][1], conditions[i][2], conditions[i][3]);
}

